package com.kanshu.ksgb.fastread.doudou.app.test


import android.annotation.SuppressLint
import android.graphics.Color
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import com.kanshu.ksgb.fastread.doudou.R
import com.kanshu.ksgb.fastread.doudou.module.reader.utils.SettingManager

class ReaderAdapter(context: ReaderActivity, list: MutableList<ReaderItem>) :
        RecyclerView.Adapter<RecyclerView.ViewHolder>() {


    var context = context
    var data = list

    //字体的大小
    var mTextSize: Int = 0

    init {
        mTextSize = SettingManager.getInstance().readFontSize
    }

    fun setFontSize() {
        //TODO title size
        mTextSize = SettingManager.getInstance().readFontSize
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == TYPE_NORMAL) ContentViewHolder(LayoutInflater.from(context).inflate(R.layout.item_reader_content, parent, false))
        else ChapterViewHolder(LayoutInflater.from(context).inflate(R.layout.item_reader_chapter, parent, false))
    }


    private val TYPE_NORMAL = 0
    private val TYPE_CHAPTER_NAME = 1
    override fun getItemViewType(position: Int): Int {
        return if (data[position].isChapterName) TYPE_CHAPTER_NAME else TYPE_NORMAL
    }

    override fun getItemCount(): Int {
        return data.size
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is ContentViewHolder -> {
                holder.tvContent.textSize = mTextSize.toFloat()
                holder.tvContent.text = "\u3000\u3000" + data[position].content
                holder.tvContent.setBackgroundColor(Color.TRANSPARENT)
                //是否展示广告
                if (data[position].showAd) {
                    holder.flAd
                    //TODO 调用广告
                }
            }

            is ChapterViewHolder -> {
                holder.tvChapterName.textSize = mTextSize.toFloat() * 1.2f
                holder.tvChapterName.text = data[position].content.trim()
            }
        }
    }

    inner class ContentViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var tvContent = view.findViewById(R.id.tvContent) as TextView
        var flAd = view.findViewById(R.id.flAd) as FrameLayout
    }

    inner class ChapterViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var tvChapterName = view.findViewById(R.id.tvChapterName) as TextView
    }

}

